package com.gxgeek.javabasic.collection.list.linkdlist;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * 分析LinkdList
 *
 */
public class AnalyzeLinkdlist {
    static final int N=50000;
    static long timeListByPrev(List list){
        long start=System.currentTimeMillis();
        Object o = new Object();
        for(int i=0;i<N;i++)
            list.add(0, o);
        return System.currentTimeMillis()-start;
    }
    static long timeListByLast(List list){
        long start=System.currentTimeMillis();
        Object o = new Object();
        for(int i=0;i<N;i++)
            list.add(i);
        return System.currentTimeMillis()-start;
    }


    public static void main(String[] args) {
        System.out.println("前置插入 ArrayList耗时："+timeListByPrev(new ArrayList()));
        System.out.println("前置插入 LinkedList耗时："+timeListByPrev(new LinkedList()));

        System.out.println("后置插入 ArrayList耗时："+timeListByLast(new ArrayList()));
        System.out.println("后置插入 LinkedList耗时："+timeListByLast(new LinkedList()));

//        removeByLinkedList();
//        removeByArrayList();
    }

    private static void removeByArrayList() {
        ArrayList linkedList = new ArrayList<>();
        for (int i = 0 ; i<100000;i++){
            linkedList.add(i);
        }
        long start = System.currentTimeMillis();
        for (int i = 99999 ; i>=0;i--){
            linkedList.remove(i);
        }
        long end = System.currentTimeMillis();
        System.out.println(end-start);

    }

    private static void removeByLinkedList() {
        LinkedList linkedList = new LinkedList();
        for (int i = 0 ; i<100000;i++){
            linkedList.add(i);
        }
        long start = System.currentTimeMillis();
        for (int i = 99999 ; i>=0;i--){
            linkedList.remove(i);
        }
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }


}
